class SearchController extends BaseController{

    def beforeInterceptor = [action:this.auth]

    def show = {
        def a = session.access;
        a.refresh()
        if(a.hasDevAuth()){
            println("Developers cannot search.");
        }
        else{
            if(a && a.emp){
                Employee e = a.emp;
                Area area = a.area;
                def type = params.searchCriteria;
                def keyword;
                if(type && type=="activityId"){
                    //keyword = Integer.parseInt(params.keyword);
                }

                //area.projects // List of projects.
                //area.courses // list of courses.
                if(params && params.keyword && type != "area"){
                    List<Project> lp = new ArrayList<Project>();
                    List<Course> lc = new ArrayList<Course>();
                    List<Project> validPrjList = new ArrayList<Project>();
                    List<Project> validCrsList = new ArrayList<Project>();
                    def cr = Project.createCriteria()
                    def resultsProjects = cr {
                            like(type, params.keyword+"%")
                            order("name", "desc")
                    }

                    def cp = Course.createCriteria()
                    def resultsCourses = cp {
                            like(type, params.keyword+"%")
                            order("name", "desc")
                    }

                    if(a.hasAdminAuth()){
                        for(Area ar in a?.container?.areas){
                            for(Project p in ar.projects){
                                validPrjList.add(p);
                            }
                            for(Course c in ar.courses){
                                validCrsList.add(c);
                            }
                        }
                    }
                    else{
                        for(Course c in area.courses){
                            validCrsList.add(c);
                        }
                        for(Project p in area.projects){
                            validPrjList.add(p);
                        }
                        validPrjList.add(area.projects);
                        validCrsList.add(area.courses);
                    }
                    
                    for(Project pr in resultsProjects){
                        if(validPrjList.contains(pr)){
                            lp.add(pr);
                        }
                    }

                    for(Course co in resultsCourses){
                        if(validCrsList.contains(co)){
                            lc.add(co);
                        }
                    }

                    if( (lp && lp.size() >0) || (lc && lc.size()>0) ){
                        flash.message = null;
                        return [courseList : lc, projectList: lp];
                    }
                    else{
                        flash.message = "No results found.";
                        return;
                    }
                }// end if not area searching
                else if(params && params.keyword && type == "area"){ // it's area searching
                    if(a.hasAdminAuth()){
                        List<Area> resArea = new ArrayList<Area>();
                        def ca = Area.createCriteria()
                        def resultsArea = ca {
                            like("name", params.keyword+"%")
                            order("name", "desc")
                        }
                        for(Area ar in a?.container.areas){
                            if(resultsArea.contains(ar)){
                                resArea.add(ar);
                            }
                        }
                        if(resArea.size()>0){
                            return [areaList : resArea];
                        }
                        else{
                             flash.message = "No results found"
                        }
                    }
                }//end params.keyword exists
                  else if(!params.keyword ){//&& !params.nothingEntered.equals("entered")
                      if(session.language.equals("sv")){
                          flash.message="Ange ett sökkriterium"
                      }
                      else{
                          flash.message="Please enter a search-criteria"
                      }
                    
                }
            }
            else{
                return;
            }
        }
        return;
    }
}
